pipeline {
    agent {
        node {
            label 'maven'
        }

    }
    stages {
        stage('拉取代码') {
            agent none
            steps {
                container('maven') {
                    git(url: 'https://gitee.com/conghuahuadan/spring-boot-demo.git', credentialsId: 'gitee', branch: 'master', changelog: true, poll: false)
                    sh 'ls -al'
                }

            }
        }

        stage('编译项目') {
            agent none
            steps {
                container('maven') {
                    sh 'mvn clean package -Dmaven.test.skip=true'
                }

                sh 'ls -al'
            }
        }

        stage('构建镜像') {
            agent none
            steps {
                container('maven') {
                    sh '''docker build -t $APPNAME:latest .
'''
                }

            }
        }

        stage('推送镜像') {
            agent none
            steps {
                container('maven') {
                    withCredentials([usernamePassword(credentialsId: 'aliyun-docker-registry', usernameVariable: 'DOCKER_USER_VAR', passwordVariable: 'DOCKER_PWD_VAR',)]) {
                        sh 'echo "$DOCKER_PWD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'
                        sh '''docker tag $APPNAME:latest $REGISTRY/$NAMESPACE/$APPNAME:$TIMESTAMP
docker push $REGISTRY/$NAMESPACE/$APPNAME:$TIMESTAMP'''
                    }

                }

            }
        }

        stage('部署项目') {
            agent none
            steps {
                container('maven') {
                    withCredentials([
                            kubeconfigFile(
                                    credentialsId: 'kubeconfig',
                                    variable: 'KUBECONFIG')
                    ]) {
                        sh 'envsubst < deploy.yml | kubectl apply -f -'
                    }

                }

            }
        }

    }

    post {
        success {
            dingtalk(
                    robot: 'test',
                    type: 'ACTION_CARD',
                    title: '部署成功',
                    text: [
                            "应用名称：[${env.JOB_NAME}](${env.JOB_URL})",
                            "\n" + "持续时间：${currentBuild.durationString}".split("and counting")[0],
                            "\n" + "执行对象：${currentBuild.buildCauses.shortDescription}",
                            "\n" + "详细信息：${env.BUILD_URL}",
                    ]
            )
        }

        failure {
            dingtalk(
                    robot: 'test',
                    type: 'ACTION_CARD',
                    title: '部署失败',
                    text: [
                            "应用名称：[${env.JOB_NAME}](${env.JOB_URL})",
                            "\n" + "持续时间：${currentBuild.durationString}".split("and counting")[0],
                            "\n" + "执行对象：${currentBuild.buildCauses.shortDescription}",
                            "\n" + "详细信息：${env.BUILD_URL}",
                    ]
            )
        }
    }

    environment {
        REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
        NAMESPACE = 'conghuahuadan'
        APPNAME = 'springbootdemo'
        APPVERSION = ''
        TIMESTAMP = "${sh(script: 'date +%s', returnStdout: true)}"
        KUBECONFIG_CREDENTIAL_ID = 'kubeconfig'
    }
}